<?php

namespace app\api\controller\v50;

use app\api\controller\v2\Auth;
use app\common\cachemodel\Recode;
use app\common\service\AstroCardService;

class AstroCard extends Auth
{
    /**
     * 星座卡片 - 首页
     * @return \think\response\Json
     * @throws \Exception
     */
    public function cardIndex()
    {
        $rid = input('rid/d', 0);
        if (empty($rid))
        {
            $rid = $this->user['rid'];
        }
        $recodeInfo = (new Recode())->getInfoById($rid);
        if (!$recodeInfo || $recodeInfo['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '当前档案不存在');
        }
        $astrCardInfo = AstroCardService::getCardDetail($recodeInfo);
        $astrCardElement = AstroCardService::fourElephantCard($recodeInfo);

        $sex = (int)$recodeInfo['type'] === 1 ? 1 : 2;
        $resData = [
            'avatar' => $recodeInfo['avatar'], // 头像
            'nickname' => $recodeInfo['name'],   // 昵称
            'card_list' => [
                [
                    'img' => "/test/astro_card/icon_sex{$sex}.png",
                    'tip' => '',
                    'title' => $astrCardInfo['user_des'],
                    'desc' => sprintf("%s %s", $astrCardInfo['surface_des'], $astrCardInfo['actual_des']),
                    'btn_name' => '准了星座属性卡',
                    'target' => 'local',
                    'link' => "astro_card?rid={$rid}&need_auth=1",
                ], [
                    'img' => '/test/astro_card/icon_card2.png',
                    'tip' => '',
                    'title' => $astrCardElement['topic'][1],
                    'desc' => $astrCardElement['topic'][0],
                    'btn_name' => '四象卡',
                    'target' => 'local',
                    'link' => "astro_card_attr?rid={$rid}&need_auth=1",
                ], [
                    'img' => '/test/astro_card/icon_card3.png',
                    'tip' => '生产日期',
                    'title' => sprintf("%d年%02d月%02d日", $recodeInfo['year'], $recodeInfo['month'], $recodeInfo['day']),
                    'desc' => '',
                    'btn_name' => '使用说明书',
                    'target' => 'local',
                    'link' => "astro_card_instruction?rid={$rid}&need_auth=1",
                ],
            ],
        ];

        return show(config('code.success'), '请求成功', $resData);
    }

    /**
     * 星座卡片 - 属性卡
     * @return \think\response\Json
     * @throws \Exception
     */
    public function cardDetail()
    {
        $rid = input('rid/d', 0);
        if (empty($rid))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $recodeInfo = (new Recode())->getInfoById($rid);
        if (!$recodeInfo || $recodeInfo['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '当前档案不存在');
        }

        $astrCardInfo = AstroCardService::getCardDetail($recodeInfo);

        $sex = (int)$recodeInfo['type'] === 1 ? 1 : 2;
        $astrCardInfo['avatar'] = "/test/astro_card/icon_sex{$sex}.png";

        return show(config('code.success'), '请求成功', $astrCardInfo);
    }

    /**
     * 星座卡片 - 四象卡
     * @return \think\response\Json
     * @throws \Exception
     */
    public function fourElephantCard()
    {
        $rid = input('rid/d', 0);
        if (empty($rid))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $recodeInfo = (new Recode())->getInfoById($rid);
        if (!$recodeInfo || $recodeInfo['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '当前档案不存在');
        }
        $astrCardElement = AstroCardService::fourElephantCard($recodeInfo);
        $attrConfig = [
            '风' => ['icon' => '/test/astro_card/风.png', 'color' => '#00E99A',],
            '火' => ['icon' => '/test/astro_card/火.png', 'color' => '#FF8636',],
            '水' => ['icon' => '/test/astro_card/水.png', 'color' => '#7775FF',],
            '土' => ['icon' => '/test/astro_card/土.png', 'color' => '#FFCA36',],
        ];
        $attrList = [];
        foreach ($astrCardElement['elem_charac'] as $item)
        {
            $attrList[] = [
                'icon' => $attrConfig[$item['elem']]['icon'],
                'name' => $item['elem'],
                'desc' => $item['text'],
                'score' => $item['perc'],
                'color' => $attrConfig[$item['elem']]['color'],
                'need_strengthen' => 0,
            ];
        }
        foreach ($astrCardElement['need_enhance'] as $item)
        {
            $attrList[] = [
                'icon' => $attrConfig[$item['elem']]['icon'],
                'name' => $item['elem'],
                'desc' => $item['text'],
                'score' => $item['perc'],
                'color' => $attrConfig[$item['elem']]['color'],
                'need_strengthen' => 1,
            ];
        }
        $resData = [
            'title1' => $astrCardElement['topic'][0],
            'title2' => $astrCardElement['topic'][1],
            'attr_list' => $attrList,
        ];
        return show(config('code.success'), '请求成功', $resData);
    }

    /**
     * 星座卡片 - 说明书
     * @return \think\response\Json
     * @throws \Exception
     */
    public function instructions()
    {
        $rid = input('rid/d', 0);
        if (empty($rid))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $recodeInfo = (new Recode())->getInfoById($rid);
        if (!$recodeInfo || $recodeInfo['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '当前档案不存在');
        }
        $astrCardInstruction = AstroCardService::instructions($recodeInfo);
        $resData = [
            'title' => '使用说明书',
            'des' => 'AN INSTRUCTION MANUAL',
            'nickname' => $recodeInfo['name'],   // 昵称
            'birthday' => sprintf("%d.%02d.%02d", $recodeInfo['year'], $recodeInfo['month'], $recodeInfo['day']),
            'sign_score' => [
                'title' => '主要成分',
                'list' => $astrCardInstruction['sign_score']
            ],
            'side_effect' => [
                'title' => '副作用',
                'list' => $astrCardInstruction['side_effect']
            ],
            'unknown_reaction' => [
                'title' => '不可预测的反应（怪癖）',
                'list' => $astrCardInstruction['oddity']
            ],
        ];

        return show(config('code.success'), '请求成功', $resData);
    }
}